package com.tianshuo.config;

import com.tianshuo.interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.util.PathMatcher;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
import org.springframework.web.servlet.resource.ResourceUrlProvider;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.util.UrlPathHelper;

import java.util.List;

/**
 * @author tianshuo
 */
@ComponentScan("com.tianshuo")
@ImportResource("classpath:spring.xml")
/**
 * 当使用@EnableWebMvc时，springmvc会引入DelegatingWebMvcConfiguration.class,并且DelegatingWebMvcConfiguration继承了
 * WebMvcConfigurationSupport.java
 * WebMvcConfigurationSupport
 * 1.初始化了参数解析器
 * 2.返回值处理器
 * 3.RequestMappingHandlerMapping
 *   BeanNameUrlHandlerMapping
 *   RouterFunctionMapping
 * 4.视图解析器
 */
@EnableWebMvc
@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/page/", ".jsp");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        converters.add(gsonHttpMessageConverter);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

//    @Bean
//    public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
//        return new SimpleControllerHandlerAdapter();
//    }
//
//    @Bean
//    public HttpRequestHandlerAdapter httpRequestHandlerAdapter(){
//        return new HttpRequestHandlerAdapter();
//    }


//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        UserInterceptor userInterceptor = new UserInterceptor();
//        registry.addInterceptor(userInterceptor).addPathPatterns("/*/user");
//    }

    @Bean
    public SimpleServletHandlerAdapter simpleServletHandlerAdapter() {
        return new SimpleServletHandlerAdapter();
    }
}
